<?php
define('IN_ECS', true);

require(dirname(__FILE__) . '/includes/init.php');
include_once(ROOT_PATH . '/includes/cls_image.php');
$exc = new exchange($ecs->table('store'), $db, 'id', 'storeName');
$image = new cls_image($_CFG['bgcolor']);
/*------------------------------------------------------ */
//-- 框架
/*------------------------------------------------------ */
if ($_REQUEST['act'] == 'list')
{
	admin_priv('store_manage');
	$smarty->assign('ur_here',      $_LANG['18_product_list']);
 	$smarty->assign('action_link',  array('href' => 'store.php?act=add', 'text' => $_LANG['02_store_add']));
 	
	$store = get_store_lists();
	$smarty->assign('store_list',  $store['row']);
	$smarty->assign('filter',       $store['filter']);
	$smarty->assign('record_count', $store['record_count']);
	$smarty->assign('page_count',   $store['page_count']);
 	$smarty->assign('full_page',    1);
 	$smarty->assign('sort_id', '<img src="images/sort_desc.gif">');
 	assign_query_info();
	$smarty->display("store.htm");
}
/*------------------------------------------------------ */
//-- 排序、分页、查询
/*------------------------------------------------------ */
elseif($_REQUEST['act']=="query"){
	$store = get_store_lists();
	$smarty->assign('store_list',  $store['row']);
	$smarty->assign('filter',       $store['filter']);
	$smarty->assign('record_count', $store['record_count']);
	$smarty->assign('page_count',   $store['page_count']);
	$sort_flag  = sort_flag($store['filter']);
	$smarty->assign($sort_flag['tag'], $sort_flag['img']);
	make_json_result($smarty->fetch('store.htm'), '',
	array('filter' => $store['filter'], 'page_count' => $store['page_count']));
}
elseif ($_REQUEST['act'] == 'edit_store_address'){
	check_authz_json('store_edit');
    $store_id       = intval($_POST['id']);
    $address         = $_POST['val'];

    if ($exc->edit("address = '$address'", $store_id))
    {
        clear_cache_files();
        make_json_result($address);
    }
}
elseif ($_REQUEST['act'] == 'edit_store_name'){
	check_authz_json('store_edit');
    $store_id       = intval($_POST['id']);
    $name         = $_POST['val'];

    if ($exc->edit("storeName = '$name'", $store_id))
    {
        clear_cache_files();
        make_json_result($name);
    }
}
elseif ($_REQUEST['act'] == 'remove')
{
	check_authz_json('store_edit');
	$id = intval($_GET['id']);
	$exc->drop($id);
	$url = 'store.php?act=query&' . str_replace('act=remove', '', $_SERVER['QUERY_STRING']);
	ecs_header("Location: $url\n");
	exit;
}
/****
 * 新增
 */
elseif($_REQUEST['act']=="add"){
	/* 权限判断 */
	admin_priv('store_edit');
	/* 初始化信息 */
	$smarty->assign('ur_here',      $_LANG['02_store_add']);
	$smarty->assign('action_link',  array('text' => $_LANG['01_store_list'], 'href'=>'store.php?act=list'));
	$smarty->assign('form_action',  'insert');
	assign_query_info();
	$smarty->display('store_info.htm');
}
elseif($_REQUEST['act']=="insert"){
	/* 权限判断 */
	check_authz_json('store_edit');
	$info['address'] = isset($_POST['address'])?$_POST['address']:"";
	$info['tel'] = isset($_POST['tel'])?$_POST['tel']:"";
	$info['storeName'] = isset($_POST['storeName'])?$_POST['storeName']:"";
	$info['longitude'] = isset($_POST['longitude'])?$_POST['longitude']:0;
	$info['latitude'] = isset($_POST['latitude'])?$_POST['latitude']:0;
	$info['star'] = isset($_POST['star'])?$_POST['star']:0;
	
	if ((isset($_FILES['dirPath']['error']) && $_FILES['dirPath']['error'] == 0) || (!isset($_FILES['dirPath']['error']) && isset($_FILES['dirPath']['tmp_name'] ) &&$_FILES['dirPath']['tmp_name'] != 'none'))
	{
		$cat_icon = basename($image->upload_image($_FILES['dirPath'], 'afficheimg'));
		 
		$info['img'] = $cat_icon;
	}
	if ((isset($_FILES['dirPath']['error']) && $_FILES['dirPath']['error'] > 0) || (!isset($_FILES['dirPath']['error']) && isset($_FILES['dirPath']['tmp_name']) && $_FILES['dirPath']['tmp_name'] == 'none'))
	{
		$link[] = array('text' => $_LANG['go_back'], 'href' => 'javascript:history.back(-1)');
		sys_msg($_LANG['js_languages']['ad_photo_empty'], 0, $link);
	}
	
	
	$db->autoExecute($ecs->table('store'), $info, 'INSERT');
	admin_log($_POST['storeName'],'add','store');
	$link[] = array('text' => $_LANG['back_list'], 'href'=>'store.php?act=list&' . list_link_postfix());
	sys_msg($_LANG['edit_succeed'],0,$link);
}

elseif ($_REQUEST['act']=="edit"){
	/* 权限判断 */
// 	admin_priv('yyg_goods');
	admin_priv('store_edit');
	$data        = get_store_info($_REQUEST['id']);
	$smarty->assign('store',               $data);
	$smarty->assign('ur_here',              $_LANG['03_store_edit']);
	$smarty->assign('action_link',          array('text' => $_LANG['01_store_list'], 'href'=>'store.php?act=list&' . list_link_postfix()));
	$smarty->assign('form_action',        'update');
	assign_query_info();
	$smarty->display('store_info.htm');

}
elseif ($_REQUEST['act'] == 'update')
{
	/* 权限判断 */
// 	admin_priv('yyg_goods');
	check_authz_json('store_edit');
	$info['address'] = isset($_POST['address'])?$_POST['address']:"";
	$info['tel'] = isset($_POST['tel'])?$_POST['tel']:"";
	$info['storeName'] = isset($_POST['storeName'])?$_POST['storeName']:"";
	$info['longitude'] = isset($_POST['longitude'])?$_POST['longitude']:0;
	$info['latitude'] = isset($_POST['latitude'])?$_POST['latitude']:0;
	$info['star'] = isset($_POST['star'])?$_POST['star']:0;
	
	if ((isset($_FILES['dirPath']['error']) && $_FILES['dirPath']['error'] == 0) || (!isset($_FILES['dirPath']['error']) && isset($_FILES['dirPath']['tmp_name'] ) &&$_FILES['dirPath']['tmp_name'] != 'none'))
	{
		$cat_icon = basename($image->upload_image($_FILES['dirPath'], 'afficheimg'));
		$info['img'] = $cat_icon;
	}
	if ((isset($_FILES['dirPath']['error']) && $_FILES['dirPath']['error'] > 0) || (!isset($_FILES['dirPath']['error']) && isset($_FILES['dirPath']['tmp_name']) && $_FILES['dirPath']['tmp_name'] == 'none'))
	{
		$link[] = array('text' => $_LANG['go_back'], 'href' => 'javascript:history.back(-1)');
		sys_msg($_LANG['js_languages']['ad_photo_empty'], 0, $link);
	}
	
	$db->autoExecute($ecs->table('store'), $info, 'UPDATE', "id = '" . $_POST['id'] . "'");
	admin_log($_POST['storeName'],'edit','store');
	$link[] = array('text' => $_LANG['back_list'], 'href'=>'store.php?act=list&' . list_link_postfix());
	sys_msg($_LANG['edit_succeed'],0,$link);
}
function get_store_lists(){
	$result = get_filter();
	if ($result === false)
	{
		$where = " WHERE 1=1 ";
		/* 查询条件 */
		$filter['keywords']   = empty($_REQUEST['keywords']) ? '' : trim($_REQUEST['keywords']);
		if (isset($_REQUEST['is_ajax']) && $_REQUEST['is_ajax'] == 1)
		{
			$filter['keywords'] = json_str_iconv($filter['keywords']);
		}
		$filter['sort_by']    = empty($_REQUEST['sort_by']) ? 'id' : trim($_REQUEST['sort_by']);
		$filter['sort_order'] = empty($_REQUEST['sort_order']) ? 'DESC' : trim($_REQUEST['sort_order']);
	
		$where .= (!empty($filter['keywords'])) ? " AND storeName like '%". mysql_like_quote($filter['keywords']) ."%'" : '';
	
		$sql = "SELECT COUNT(*) FROM " . $GLOBALS['ecs']->table('store'). $where;
		$filter['record_count'] = $GLOBALS['db']->getOne($sql);
	
		$filter = page_and_size($filter);
	
		/* 获活动数据 */
		$sql = "SELECT * FROM " . $GLOBALS['ecs']->table('store') . $where.
				" ORDER by $filter[sort_by] $filter[sort_order] LIMIT ". $filter['start'] .", " . $filter['page_size'];
	
		$filter['keywords'] = stripslashes($filter['keywords']);
		set_filter($filter, $sql);
	}
	else
	{
		$sql    = $result['sql'];
		$filter = $result['filter'];
	}
	
	$row = $GLOBALS['db']->getAll($sql);
	$arr = array('row' => $row, 'filter' => $filter, 'page_count' => $filter['page_count'], 'record_count' => $filter['record_count']);
	return $arr;
	
}

function get_store_info($id){
	$where = " WHERE id='$id'";
	/* 获得活动数据 */
	$sql = "SELECT * FROM " . $GLOBALS['ecs']->table('store') . $where;
	$data = $GLOBALS['db']->GetRow($sql);
	$data['img'] = "/data/afficheimg/".$data['img'];
	return $data;
}